package com.acme;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddClientGUI extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField tbName;
	private JTextField tbBalance;
	private JTextField tbOverdraft;
	private JButton btnSend;
	private JComboBox<?> cbGender;
	private JComboBox<?> cbAccType;
	private AddClientValidator validator;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddClientGUI frame = new AddClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public AddClientGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		setBounds(100, 100, 207, 206);
		validator = new AddClientValidator();
		initComponents();
		setHandlers();
	}
	
	private void initComponents() {
		//name
		JLabel lblName = new JLabel("name");
		tbName = new JTextField();
		tbName.setColumns(10);
		
		//gender
		JLabel lblGender = new JLabel("gender");
		cbGender = new JComboBox<Object>(Gender.values());
		
		//account type
		JLabel lblAccType = new JLabel("account type");
		cbAccType = new JComboBox<Object>(AccountType.values());
		
		//balance
		JLabel lblBalance = new JLabel("balance");
		tbBalance = new JTextField();
		tbBalance.setColumns(10);
		
		//overdraft
		JLabel lblOverdraft = new JLabel("overdraft");
		tbOverdraft = new JTextField();
		tbOverdraft.setColumns(10);
		
		//button send
		btnSend = new JButton("send");
				
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(tbName, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
										.addComponent(cbGender, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblGender))
									.addPreferredGap(ComponentPlacement.UNRELATED))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblBalance)
									.addPreferredGap(ComponentPlacement.RELATED))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(tbBalance, 0, 0, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.RELATED)))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addComponent(tbOverdraft, 0, 0, Short.MAX_VALUE)
								.addComponent(lblOverdraft)
								.addComponent(lblAccType)
								.addComponent(cbAccType, 0, 84, Short.MAX_VALUE))))
					.addGap(127))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(51)
					.addComponent(btnSend, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(87, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblName)
					.addContainerGap(284, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(lblName)
					.addGap(2)
					.addComponent(tbName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblGender)
						.addComponent(lblAccType))
					.addGap(2)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(cbGender, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(cbAccType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(4)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOverdraft)
						.addComponent(lblBalance))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(tbBalance, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(tbOverdraft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnSend)
					.addContainerGap(25, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}
	
	private void setHandlers() {
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sendToClient();
			}
		});
	}
	
	private void sendToClient() {
		validateFields();
		String feedString = generateFeedString();
		new BankClient().start(feedString);
	}
	
	private void validateFields() {
		validator.validateName(this.tbName.getText());
		double balance = Double.parseDouble(this.tbBalance.getText());
		validator.validateBalance(balance);
		double overdraft = Double.parseDouble(this.tbOverdraft.getText());
		validator.validateOverdraft(overdraft);
	}
	
	private String generateFeedString() {
		char gender = 0;
		int cbGenderSelectedIndex = this.cbGender.getSelectedIndex();
		if(cbGenderSelectedIndex == 0) {
			gender = 'm';
		}
		else {
			gender = 'f';
		}
		
		char accType = 0;
		int cbAccTypeSelectedIndex = this.cbGender.getSelectedIndex();
		if(cbAccTypeSelectedIndex == 0) {
			accType = 's';
		}
		else {
			accType = 'c';
		}
		
		double balance = Double.parseDouble(this.tbBalance.getText());
		String name = this.tbName.getText();
		double overdraft = Double.parseDouble(this.tbOverdraft.getText());;
		return String.format("accounttype=%c;balance=%3.2f;overdraft=%3.2f;name=%s;gender=%c;", accType,
				balance, overdraft, name, gender);
	}
}
